package com.example.oversea_home_spring.controller;


import com.example.oversea_home_spring.bean.*;
import com.example.oversea_home_spring.service.ManagementService;
import com.example.oversea_home_spring.service.OwnerService;
import com.example.oversea_home_spring.uitl.CodeMsg;
import com.example.oversea_home_spring.uitl.DateUtil;
import com.example.oversea_home_spring.uitl.Result;
import com.example.oversea_home_spring.uitl.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin//跨域
@RequestMapping("owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @Autowired
    private ManagementService managementService;


    //仓库的 增删查改
    //申请仓库 add
    @PostMapping("addhouse")
    public Result<Warehouse> addHouse(Warehouse warehouse){
        if(warehouse.getUserId()==null){
            return Result.error(CodeMsg.LOGIN_ID_ERROR);
        }
        if(warehouse.getSumContain() < 0 || warehouse.getNowContain() < 0){
            return Result.error(CodeMsg.HOUSE_ADD_ERROR);
        }
        //直接添加
        warehouse.setId(UUIDUtil.uuid());
        warehouse.setStatus((long) 1);
        warehouse.setOrderNum((long)0);
        warehouse.setCreate_time(DateUtil.myDate());
        warehouse.setUpdate_time(DateUtil.myDate());
        int addone = ownerService.addHouse(warehouse);
        if(addone > 0){
            return Result.success(warehouse);
        }
        else {
            return Result.error(CodeMsg.HOUSE_ADD_ERROR);
        }
    }

    //查询仓库
    @GetMapping("myhouse")
    public Result<List<Warehouse>> getHouses(String userId){
        List<Warehouse> list = ownerService.getHouses(userId);
        System.out.println(list);
        if(list.size()>0){
            return Result.success(list);
        }
        else return Result.error(CodeMsg.HOUSE_ElECT_EXIST);
    }

    //更新仓库
    @PutMapping("updatehouse")
    public Result<Integer> updateHouse(Warehouse warehouse){
        warehouse.setUpdate_time(DateUtil.myDate());
        System.out.println(warehouse.getMoney());
        warehouse.setStatus((long)1);
        int isOk = ownerService.updateHouse(warehouse);
        if(isOk > 0){
            return Result.success(isOk);
        }
        else {
            return Result.error(CodeMsg.HOUSE_UPDATE_ERROR);
        }
    }

    //删除仓库
    @DeleteMapping("deletehouse")
    public Result<Integer> deleteHouse(String id){
        if(id!=""){
            int isOK = ownerService.deleteHouse(id);
            if(isOK > 0){
                return Result.success(isOK);
            }
        }
        return Result.error(CodeMsg.HOUSE_DELETE_ERROR);
    }


    //员工的 增删查改
    //添加员工-->通过仓库来完成
    @PutMapping("addWorker")
    public Result<Integer> addWorker(String wareHouseId,String workerId){
        Management management = new Management();
        management.setCreate_time(DateUtil.myDate());
        management.setUpdate_time(DateUtil.myDate());
        management.setId(UUIDUtil.uuid());
        management.setWarehouseId(wareHouseId);
        management.setWorkId(workerId);
        management.setStatus((long) 1);
        //todo 添加判断id是否正常
        int isOk =  managementService.addOneWorker(management);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.WORKER_ADD_ERROR);
    }


    //查询员工
    @GetMapping("myWorker")
    public Result<List<User>> selectAllMyWorker(String userId){
        List<User> list = ownerService.selectAllMyWorker(userId);
        System.out.println(list);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.WORKER_SELECT_ERROR);
    }


    //修改员工信息
    @PutMapping("updateWorker")
    public Result<Integer> updateWorkerInfo(Management management){
        management.setUpdate_time(DateUtil.myDate());
        int isok = ownerService.updateWorkerInfo(management);
        if( isok > 0){
            return Result.success(isok);
        }
        return Result.error(CodeMsg.UPDATE_WORKER_ERROR);
    }




    //租客的 增删查改
    //查询租客
    @GetMapping("myTrader")
    public Result<List<User>> selectAllMyTrader(String userId){
        List<User> list = ownerService.selectAllMyTrader(userId);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.TRADER_SELECT_ERROR);
    }

    //查询租客的仓库请求
    @GetMapping("myHouseOrder")
    public Result<List<Rent>> selectAllMyHouseOrder(String userId){
        List<Rent> list = ownerService.selectAllMyHouseOrder(userId);
        if(list!=null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.TRADER_SELECT_ERROR);
    }

    //员工的 增删查改
    //todo 未测试 仓库批准使用
    @PutMapping("updateRent")
    public Result<Integer> updateOneRent(Rent rent){
        rent.setUpdate_time(DateUtil.myDate());
        int isOk = ownerService.updateOneRent(rent);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.HOUSE_RENT_ERROR);
    }




}
